package OOPExercise;

import java.util.Scanner;

public class PolymorphicDemo {
    static String name;
    static int age;
    static String color;
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        Animal a = new Dog();
        a.eat();
        System.out.println("输入你要的动物总数");
        int n = sc.nextInt();
        Animal[] animal = new Animal[n];
        animal = getArr(n);//录入并获取动物属性
        print(animal);//打印各方法
    }

    public static Animal[] getArr(int n) {
        Animal[] animal = new Animal[n];

        for (int i = 0; i < animal.length; i++) {//向上造型(超类型的引用可以自动类型转换为子类)
            System.out.println("输入狗的名字，年龄，颜色");
            name = sc.next();
            age = sc.nextInt();
            color = sc.next();
            animal[i] = new Dog(name, age, color);
            i++;
            System.out.println("输入鸡的名字，年龄，颜色");
            name = sc.next();
            age = sc.nextInt();
            color = sc.next();
            animal[i] = new Chick(name, age, color);
            i++;
            System.out.println("输入鱼的名字，年龄，颜色");
            name = sc.next();
            age = sc.nextInt();
            color = sc.next();
            animal[i] = new Fish(name, age, color);
        }
        return animal;
    }

    public static void print(Animal[] animal) {
        for (int i = 0; i < animal.length; i++) {//向下造型，输出特有方法
            if (animal[i] instanceof Dog) {
                System.out.println(animal[i].name);
                animal[i].eat();
                animal[i].drink();
                Master.feed(animal[i]);
                System.out.println("特有类：");
                ((Dog) (animal[i])).lookHome();

            }
            if (animal[i] instanceof Swim) {
                System.out.println(animal[i].name);
                if (animal[i] instanceof Fish) {
                    animal[i].drink();//前面狗喝过了区别狗和鱼
                }
                ((Swim) animal[i]).swim();
                Master.feed(animal[i]);
            }
            if (animal[i] instanceof Chick) {
                System.out.println(animal[i].name);
                animal[i].drink();
                System.out.println("特有类：");
                ((Chick) animal[i]).layEggs();
                Master.feed(animal[i]);
            }
        }

    }
}





